import os.path
import sys
import matplotlib.pyplot as plt
import time
from tqdm import tqdm
import numpy as np


def count_target_in_list(target, l):
    i = 0
    for item in l:
        if item == target:
            i = i + 1
    return i


def get_index_of_char_in_list(c, l):
    indexes = []
    i = 0
    while i < len(l):
        if l[i] == c:
            indexes.append(i)
        i = i+1
    return indexes


def analyze_workload(file_path, write_path, plot_dir):
    dataname = file_path.split('/')[-1]
    print('开始处理%s...' % dataname)

    with open(file_path, 'r') as f1:
        lines = f1.readlines()

    f = open(write_path, 'w')
    count_all = len(lines)
    print('%s读取完成，包含条目%d' % (dataname, count_all))

    count_load = int(count_all * 3.0 / 5.0)
    count_run = count_all - count_load

    f.writelines(file_path + '\n')
    f.writelines("num of keys in all-%d\n" % count_all)
    f.writelines("num of keys in load-%d\n" % count_load)
    f.writelines("num of keys in run-%d\n" % count_run)

    flags = []
    keys = []
    for line in tqdm(lines):
        flag = line.split(",")[1]
        key = int(line.split(",")[2])
        flags.append(flag)
        keys.append(key)

    # 先分析整体的load
    flags_load = flags[:count_load]
    load_read = count_target_in_list('R', flags_load)
    load_write = count_target_in_list('W', flags_load)

    read_ratio_load = load_read / count_load
    f.writelines("read_number_inload-%d\n" % load_read)
    f.writelines("read_ratio_inload-%.3f\n" % read_ratio_load)

    write_ratio_load = load_write / count_load
    f.writelines("write_number_inload-%d\n" % load_write)
    f.writelines("write_ratio_inload-%.3f\n" % write_ratio_load)

    # 再分析整体的run
    flags_run = flags[count_load:]
    run_read = count_target_in_list('R', flags_run)
    run_write = count_target_in_list('W', flags_run)

    read_ratio_run = run_read / count_run
    f.writelines("read_number_inrun-%d\n" % run_read)
    f.writelines("read_ratio_inrun-%.3f\n" % read_ratio_run)

    write_ratio_run = run_write / count_run
    f.writelines("write_number_inrun-%d\n" % run_write)
    f.writelines("write_ratio_inrun-%.3f\n" % write_ratio_run)

    # 分析分开的run阶段
    flags_run = flags[count_load:]

    # 先统计run被分成10份的
    num_ops_period = int(len(flags_run) * 0.1)
    f.writelines("\nall_number_period-all10-%d\n" % num_ops_period)
    for i in range(10):
        if (i+1)*num_ops_period <= len(flags_run):
            flags_i = flags_run[i*num_ops_period: (i+1)*num_ops_period]
            flags_i_read = count_target_in_list('R', flags_i)
            flags_i_write = count_target_in_list('W', flags_i)
            assert (flags_i_read+flags_i_write == num_ops_period)
            flags_i_read_ratio = flags_i_read/num_ops_period
            flags_i_write_ratio = flags_i_write/num_ops_period

            f.writelines("\nread_number_period-all10-%d-%d\n" % (i, flags_i_read))
            f.writelines("read_ratio_period-all10-%d-%.3f\n" % (i, flags_i_read_ratio))
            f.writelines("write_number_period-all10-%d-%d\n" % (i, flags_i_write))
            f.writelines("write_ratio_period-all10-%d-%.3f\n" % (i, flags_i_write_ratio))
        else:
            raise ValueError

    # 再统计run被分成5份的
    num_ops_period = int(len(flags_run) * 0.2)
    f.writelines("\nall_number_period-all5-%d\n" % num_ops_period)
    for i in range(5):
        if (i+1)*num_ops_period <= len(flags_run):
            flags_i = flags_run[i*num_ops_period: (i+1)*num_ops_period]
            flags_i_read = count_target_in_list('R', flags_i)
            flags_i_write = count_target_in_list('W', flags_i)
            assert (flags_i_read+flags_i_write == num_ops_period)
            flags_i_read_ratio = flags_i_read/num_ops_period
            flags_i_write_ratio = flags_i_write/num_ops_period

            f.writelines("\nread_number_period-all5-%d-%d\n" % (i, flags_i_read))
            f.writelines("read_ratio_period-all5-%d-%.3f\n" % (i, flags_i_read_ratio))
            f.writelines("write_number_period-all5-%d-%d\n" % (i, flags_i_write))
            f.writelines("write_ratio_period-all5-%d-%.3f\n" % (i, flags_i_write_ratio))
        else:
            raise ValueError
    f.close()

    # plt.figure()
    # sample_interval = 10000
    # keys_sample = keys[::sample_interval]
    # flags_sample = flags[::sample_interval]
    # xs = range(len(keys))[::sample_interval]
    #
    # sample_read_indexes = get_index_of_char_in_list('R', flags_sample)
    # sample_write_indexes = get_index_of_char_in_list('W', flags_sample)
    # keys_sample_read = np.array(keys_sample)[sample_read_indexes]
    # keys_sample_write = np.array(keys_sample)[sample_write_indexes]
    # xs_sample_read = np.array(xs)[sample_read_indexes]
    # xs_sample_write = np.array(xs)[sample_write_indexes]
    #
    # plt.scatter(xs_sample_read, keys_sample_read, color='b', label='read')
    # plt.scatter(xs_sample_write, keys_sample_write, color='r', label='write')
    # plt.legend()
    # plt.savefig(plot_dir + 'plot_' + file_path.split('/')[-1].split('.')[0] + '.png')
    # # plt.show()
    # plt.close()


if __name__ == "__main__":
    st = time.time()

    file_path = sys.argv[1]
    data_name = file_path.split('/')[-1].split('.')[0]
    write_dir = "../evaluation_analyse/"  # 养成判断父文件夹是否存在的习惯
    plot_dir = '../evaluation_plot/'

    write_path = write_dir + "analyse_" + data_name + '.txt'

    # if not os.path.exists(write_path):
    #     analyze_workload(file_path, write_path, plot_dir)
    # else:
    #     print('skip analysing %s' % file_path.split('/')[-1])

    analyze_workload(file_path, write_path, plot_dir)

    elps = time.time() - st
    print('%s run for %.1f s' % (write_path.split('/')[-1], elps))
